{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LinearRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "nameList=[\"600188\",'600348','600546','601666','601898']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getCoef(name):\n",
    "    csv_data = pd.read_csv(name+'.csv', usecols=['ret', 'risk', 'smb', 'hml'])\n",
    "    csv_data = csv_data.fillna(method='ffill')\n",
    "    y=csv_data['ret'].values\n",
    "    x=csv_data[['risk','smb','hml']].values\n",
    "    model = LinearRegression()\n",
    "    model.fit(x,y)\n",
    "    return model.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "allCoef=[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in nameList:\n",
    "    allCoef.append(getCoef(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "allCoef=np.array(allCoef)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mark(col):\n",
    "    score=[2,4,6,8,10] # 和col长度相同\n",
    "    sortList = sorted(enumerate(col), key=lambda x: x[1]) # 前面是原来下标，后面是原来元素值，现在的索引是顺序（从小到大）\n",
    "    for i in range(len(col)):\n",
    "        oldSub,_=sortList[i]\n",
    "        col[oldSub]=score[i] # score里的排序和顺序一样\n",
    "    return col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(3): # 3个因子\n",
    "    mark(allCoef[:,i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "allScore=[]\n",
    "for i in range(5): # 5个股票\n",
    "    allScore.append(np.mean(allCoef[i,:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['600188', '600348', '600546', '601666', '601898']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nameList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6.0, 6.666666666666667, 8.0, 6.0, 3.3333333333333335]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "allScore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
